package it.niedermann.android.markdown.markwon.format

import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
import com.gitee.wsl.android.ext.device.ClipboardExt.getClipboardURLorNull
import io.noties.markwon.base.R
import it.niedermann.android.markdown.MarkdownUtil
import it.niedermann.android.markdown.markwon.MarkwonMarkdownEditor
import it.niedermann.android.markdown.model.EListType
import timber.log.Timber

class ContextBasedFormattingCallback(private val editText: MarkwonMarkdownEditor) :
    ActionMode.Callback {
    override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
        mode.menuInflater.inflate(R.menu.context_based_formatting, menu)
        return true
    }

    override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
        val text = editText.text
        if (text != null) {
            val cursorPosition = editText.selectionStart
            if (cursorPosition >= 0 && cursorPosition <= text.length) {
                val startOfLine = MarkdownUtil.getStartOfLine(text, cursorPosition)
                val endOfLine = MarkdownUtil.getEndOfLine(text, cursorPosition)
                val line = text.subSequence(startOfLine, endOfLine).toString()
                if (MarkdownUtil.lineStartsWithCheckbox(line)) {
                    menu.findItem(R.id.checkbox).isVisible = false
                    Timber.tag(TAG)
                        .i("Hide checkbox menu item because line starts already with checkbox")
                }
            } else {
                Timber.tag(TAG)
                    .e("SelectionStart is $cursorPosition  Expected to be between 0 and ${text.length}")
            }
        }
        return false
    }

    override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
        val editable = editText.text
        if (editable != null) {
            val itemId = item.itemId
            val cursorPosition = editText.selectionStart
            if (itemId == R.id.checkbox) {
                editable.insert(
                    MarkdownUtil.getStartOfLine(editable, cursorPosition),
                    EListType.DASH.checkboxUncheckedWithTrailingSpace
                )
                editText.setMarkdownStringModel(editable)
                editText.setSelection(cursorPosition + EListType.DASH.checkboxUncheckedWithTrailingSpace.length)
                return true
            } else if (itemId == R.id.link) {
                val newSelection = MarkdownUtil.insertLink(
                    editable,
                    cursorPosition,
                    cursorPosition,
                    getClipboardURLorNull()
                )
                editText.setMarkdownStringModel(editable)
                editText.setSelection(newSelection)
                return true
            }
        } else {
            Timber.tag(TAG).e("Editable is null")
        }
        return false
    }

    override fun onDestroyActionMode(mode: ActionMode) {
        // Nothing to do here...
    }

    companion object {
        private val TAG = ContextBasedFormattingCallback::class.java.simpleName
    }
}